package com.doitedu.mr.day07.skew;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

/**
 * @Date 2021/12/9
 * @Created by HANGGE
 * @Description TODO
 */
public class Skew2 {
    static  class  Skew2Mapper extends Mapper<LongWritable, Text, Text , IntWritable>{
Text k = new  Text();
IntWritable v = new IntWritable();
        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            try {
                String line = value.toString();
                String word = line.split("-")[0];
                String cnt = line.split("\t")[1];
                k.set(word);
                v.set(Integer.parseInt(cnt));
                context.write(k,v);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    static  class Skew2Reducer extends Reducer<Text, IntWritable , Text, IntWritable> {
        IntWritable v = new IntWritable() ;
        @Override
        protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
            int  cnt = 0 ;
            for (IntWritable value : values) {
                cnt+=value.get() ;
            }
            v.set(cnt);
            context.write(key , v);
        }
    }

    public static void main(String[] args) throws  Exception {
        // Job  统计文件中单词出现的次数
        Configuration conf = new Configuration();
        // 1  创建 JOB
        Job job = Job.getInstance(conf, "skew");
        // 1) Mapper 类
        job.setMapperClass(Skew2Mapper.class);
        // 2) Reducer类
        job.setReducerClass(Skew2Reducer.class);


        // 4)  设置reduce输出的数据类型
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);
        // 设置两个reducetask
        //job.setNumReduceTasks(2);

        // 5) 设置输入的数据路径
        FileInputFormat.setInputPaths(job , new Path("E:\\mrdata\\skew\\res2"));
        // 6) 设置输出的结果保存路径
        FileOutputFormat.setOutputPath(job, new Path("E:\\mrdata\\skew\\res"));
        // 2 提交工作
        // 等待执行完成 , 打印执行过
        job.waitForCompletion(true) ;




    }
}
